package main

import (
	"fmt"
	"os"
	"os/signal"
	xcanal "server/dbtools/xcanal/canal"
	"syscall"
)


func main() {

	cfg := xcanal.NewDefaultConfig()
	fmt.Printf("创建canal配置文件(默认)：%+v\n", cfg)

	cfg.Addr = "192.168.3.61:3306"
	cfg.User = "canal" // --net reload,select,replication client,replication slave
	cfg.Password = "canal"
	cfg.Flavor = "mysql" // --db type
	cfg.UseDecimal = true

	cfg.Dump.ExecutionPath = "C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysqldump" // --执行文件的路径
	cfg.Dump.DiscardErr = false

	fmt.Printf("创建canal配置文件(修改)：%+v\n", cfg)

	c, err := xcanal.NewCanal(cfg)
	if err != nil {
		fmt.Printf("create canal err %v\n", err)
		os.Exit(1)
	}

	err = c.Dump()
	if err != nil {
		println("canal dump err:", err.Error())
		return
	}



	c.SetEventHandler(&handler{})
	pos, err := c.GetMasterPos()
	if err != nil {
		return
	}
	fmt.Printf("%v\n", pos)
	startPos := pos

	fmt.Printf("修改canal：%+v\n", c)
	go func() {
		err = c.RunFrom(startPos)
		if err != nil {
			fmt.Printf("start canal err: %v\n", err)
		}
	}()
	c.AddIncludeTable("book.user")
	sc := make(chan os.Signal, 1)
	signal.Notify(sc,
		os.Kill,
		os.Interrupt,
		syscall.SIGHUP,
		syscall.SIGINT,
		syscall.SIGTERM,
		syscall.SIGQUIT)

	<-sc

	c.Close()

}

type handler struct {
	xcanal.DummyEventHandler
}

func (h *handler) OnRow(e *xcanal.RowsEvent) error {
	fmt.Printf("---OnRow:%+v\n", e)

	return nil
}

func (h *handler) String() string {
	return "TestHandler"
}
